package com.qk.server.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.Dto.UserDto;
import com.qk.Vo.LoginResultVo;
import com.qk.common.PageResult;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.server.UserServer;
import com.qk.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServerImpl implements UserServer {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult findUserByList(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        List<User> userList = userMapper.selectUserByList(userDto);
        Page<User>page1=(Page<User>) userList;
        return new PageResult<>(page1.getTotal(),page1.getResult());
    }

    @Override
    public void addUser(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String str = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(str.getBytes()));
        userMapper.insertUser(user);
    }

    @Override
    public void deleteUserByIds(List<Integer> ids) {
        userMapper.deleteUserByIds(ids);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        String str =user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(str.getBytes()));
        userMapper.updateUser(user);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public List<User> findUserByRole(String roleLabel) {
        return userMapper.selectUserByRole(roleLabel);
    }

    @Override
    public void findUserAll() {
        userMapper.selectUserAll();
    }

    @Override
    public List<User> findUserByDept(Integer deptId) {
        return userMapper.findUserByDept(deptId);
    }

    @Override
    public LoginResultVo loginUser(User user) {
        User u = userMapper.loginUser(user.getUsername());
        log.info("用户信息：{}",u);
        //判断用户是否存在
        if(u==null){
            throw new BusinessException("用户或密码错误");
        }
        //判断密码是否正确
        if(!u.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
            throw new BusinessException("用户或密码错误");
        }
        //判断状态是否正常
        if(u.getStatus()==0){
            throw new BusinessException("用户已停用");
        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        loginResultVo.setToken(JwtUtils.generateToken(claims));
        return loginResultVo;
    }
}
